<script setup>
import PageContainer from '@/components/pageContainer.vue'
import { ref } from 'vue'
import { userUpdateInfoService } from '@/api/user'
import { useUserStore } from '@/stores/index.js'
// 表单引用
const formRef = ref(null)
const {
  user: { email, id, nickname, username },
  getUser
} = useUserStore()
// 表单数据
const formData = ref({
  id,
  username,
  nickname,
  email
})
console.log(formData.value)
// 表单验证规则
const formRules = ref({
  nickname: [
    { required: true, message: '用户昵称不能为空', trigger: 'blur' },
    {
      min: 2,
      max: 10,
      message: '用户昵称长度必须在2到10个字符之间',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '用户邮箱不能为空', trigger: 'blur' },
    {
      type: 'email',
      message: '请输入正确的邮箱格式',
      trigger: 'blur'
    }
  ]
})
const submitForm = async () => {
  await formRef.value.validate()
  await userUpdateInfoService(formData.value)
  getUser()
  ElMessage.success('修改成功')
}
</script>
<template>
  <PageContainer title="基本资料">
    <!--基本资料表单-->
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      class="user-profile-form"
    >
      <el-form-item label="登录名称" prop="username">
        <el-input
          v-model="formData.username"
          placeholder="请输入登录名称"
          disabled
        />
      </el-form-item>

      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="formData.nickname"
          placeholder="请输入用户昵称（2-10位）"
        />
      </el-form-item>

      <el-form-item label="用户邮箱" prop="email">
        <el-input
          v-model="formData.email"
          placeholder="请输入用户邮箱"
          type="email"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm" class="submit-btn">
          提交修改
        </el-button>
      </el-form-item>
    </el-form>
  </PageContainer>
</template>
<style scoped></style>
